package com.uchengwang.ui.cm_ui.rychat.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.SwitchCompat;
import android.text.TextUtils;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.uchengwang.R;
import com.uchengwang.functions.http.config.BaseConfig;
import com.uchengwang.functions.http.listener.OnHttpResultSingleListener;
import com.uchengwang.functions.http.listener.OnResultListener;
import com.uchengwang.functions.image.ImageManager;
import com.uchengwang.ui.base_ui.ui.BaseWithTitleActivity;
import com.uchengwang.ui.cm_ui.login.bean.UserBean;
import com.uchengwang.ui.cm_ui.me.presenter.HttpMeManager;
import com.uchengwang.ui.cm_ui.me.ui.EditorInfoActivity;
import com.uchengwang.ui.cm_ui.rychat.manager.RongChatManager;
import com.uchengwang.ui.cm_ui.ucity.ui.ReportActivity;
import com.uchengwang.ui.utils.DialogUtils;
import com.uchengwang.ui.utils.UCommonUtil;

import io.rong.imkit.RongIM;
import io.rong.imlib.RongIMClient;
import io.rong.imlib.model.Conversation;

/**
 * Created by U城网 on 2017/7/17.
 * 个人聊天设置
 */

public class ChatPersonSettingActivity extends BaseWithTitleActivity implements View.OnClickListener, CompoundButton.OnCheckedChangeListener{
    private RelativeLayout per_info, clear, report, chatSet_remark;
    private SwitchCompat msg_bt, shield_bt;
    private ImageView head;
    private TextView name;

    private String nickName;
    private long uid;
    private AlertDialog dialog;
    private boolean isMsgInit = true, isSheildInit = true;
    private String remarkName;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_person_chat_setting);
        setTitle("聊天设置");

        nickName = getIntent().getStringExtra(BaseConfig.NICK_NAME);
        uid = getIntent().getLongExtra(BaseConfig.UID, 0);

        initView();
    }

    private void initView(){
        head = (ImageView) findViewById(R.id.chatSet_head);
        name = (TextView) findViewById(R.id.chatSet_name);
        per_info = (RelativeLayout) findViewById(R.id.chatSet_per_info);
        per_info.setOnClickListener(this);

        clear = (RelativeLayout) findViewById(R.id.chatSet_clear);
        clear.setOnClickListener(this);
        report = (RelativeLayout) findViewById(R.id.chatSet_report);
        report.setOnClickListener(this);

        chatSet_remark = (RelativeLayout) findViewById(R.id.chatSet_remark);
        chatSet_remark.setOnClickListener(this);

        msg_bt = (SwitchCompat) findViewById(R.id.chatSet_msg_bt);///消息免打扰
        msg_bt.setOnCheckedChangeListener(this);
        shield_bt = (SwitchCompat) findViewById(R.id.chatSet_shield_bt);
        shield_bt.setOnCheckedChangeListener(this);

        HttpMeManager.getPresenter().getUserInfo(this, uid, new OnHttpResultSingleListener<UserBean>() {
            @Override
            public void onResult(int status, String msg, UserBean userBean) {
                if (userBean != null){
                    if (userBean.isFriend() || userBean.isFollow()){
                        chatSet_remark.setVisibility(View.VISIBLE);
                        remarkName = userBean.getRemarkName();
                    }
                    if (TextUtils.isEmpty(userBean.getUserAvatar())){
                        ImageManager.getInstance().loadImage(ChatPersonSettingActivity.this, head, R.drawable.no_head, ImageManager.ImageType.CIRCLE);
                    }else {
                        ImageManager.getInstance().loadImage(ChatPersonSettingActivity.this, head, userBean.getUserAvatar(), ImageManager.ImageType.CIRCLE);
                    }
                    name.setText(!TextUtils.isEmpty(userBean.getRemarkName()) ? userBean.getRemarkName() : userBean.getNickName());
                }
            }
        });


        getBlacks();//检查用户是否在黑名单里

        /**
         * 查询这个ID有没有在消息免打扰里
         * */
        RongIM.getInstance().getConversationNotificationStatus(Conversation.ConversationType.PRIVATE, String.valueOf(uid), new RongIMClient.ResultCallback<Conversation.ConversationNotificationStatus>() {
            @Override
            public void onSuccess(Conversation.ConversationNotificationStatus s) {
                if (s == Conversation.ConversationNotificationStatus.DO_NOT_DISTURB){
                    //消息免打扰
                    msg_bt.setChecked(true);
                }else {
                    isMsgInit = false;
                }
            }

            @Override
            public void onError(RongIMClient.ErrorCode errorCode) {

            }
        });

    }

    @Override
    public void onClick(View view) {
        Intent intent = null;

        switch (view.getId()){
            case R.id.chatSet_per_info://点击进入个人主页
                UCommonUtil.getUtil().toUserActivity(this, uid);

                break;
            case R.id.chatSet_clear:
                //清空聊天消息
                dialog = DialogUtils.getUtils().showDialogSingle(this, "确定要清除和" + nickName + "的聊天记录吗?", "确定", new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        DialogUtils.getUtils().cancel();
                        RongIM.getInstance().clearMessages(Conversation.ConversationType.PRIVATE, String.valueOf(uid), new RongIMClient.ResultCallback<Boolean>() {
                            @Override
                            public void onSuccess(Boolean aBoolean) {
                                if (aBoolean){
                                    UCommonUtil.getUtil().showToast(ChatPersonSettingActivity.this, "清除成功");
                                }else {
                                    UCommonUtil.getUtil().showToast(ChatPersonSettingActivity.this, "清除失败, 请重试!");
                                }
                            }

                            @Override
                            public void onError(RongIMClient.ErrorCode errorCode) {
                                UCommonUtil.getUtil().showToast(ChatPersonSettingActivity.this, "清除失败, 请重试!");
                            }
                        });
                    }
                });

                break;

            case R.id.chatSet_remark:
                //设置备注
                Intent in = new Intent(this, EditorInfoActivity.class);
                in.putExtra(BaseConfig.TYPE_STRING, remarkName);
                in.putExtra(BaseConfig.NICK_NAME, nickName);
                in.putExtra(BaseConfig.TYPE, 5);
                in.putExtra(BaseConfig.REMARK_UID, uid);
                startActivityForResult(in, 0);
                break;

            case R.id.chatSet_report:
                //举报 个人
                intent = new Intent(this, ReportActivity.class);
                intent.putExtra(BaseConfig.TYPE, BaseConfig.REPORT_PERSON_TYPE);
                intent.putExtra(BaseConfig.ID, uid);
                break;
        }
        if (intent != null){
            startActivity(intent);
        }
    }

    @Override
    public void onCheckedChanged(CompoundButton compoundButton, final boolean b) {
        switch (compoundButton.getId()){
            case R.id.chatSet_msg_bt:
                //消息免打扰
                if (isMsgInit){
                    isMsgInit = false;
                    return;
                }
                //消息免打扰
                Conversation.ConversationNotificationStatus status = null;
                if (b){
                    //打开消息免打扰
                    status = Conversation.ConversationNotificationStatus.DO_NOT_DISTURB;
                }else {
                    status = Conversation.ConversationNotificationStatus.NOTIFY;
                }
                RongIM.getInstance().setConversationNotificationStatus(Conversation.ConversationType.PRIVATE, String.valueOf(uid), status, new RongIMClient.ResultCallback<Conversation.ConversationNotificationStatus>() {
                    @Override
                    public void onSuccess(Conversation.ConversationNotificationStatus conversationNotificationStatus) {
                    }

                    @Override
                    public void onError(RongIMClient.ErrorCode errorCode) {

                    }
                });
                break;
            case R.id.chatSet_shield_bt:
                //屏蔽 [拉入黑名单]
                if (isSheildInit){
                    isSheildInit = false;
                    return;
                }
                if (b){
                    //加入黑名单
                    RongChatManager.getUtils().addOrRemoveBlacklist(true, uid, new OnResultListener() {
                        @Override
                        public void onResult(int stutas, String msg, int total, String data) {
                            if (stutas ==0){
                                UCommonUtil.getUtil().showToast(ChatPersonSettingActivity.this, "屏蔽成功");
                            }else {
                                UCommonUtil.getUtil().showToast(ChatPersonSettingActivity.this, "屏蔽失败");
                            }
                        }
                    });
                }else {
                    //从黑名单放出
                    RongChatManager.getUtils().addOrRemoveBlacklist(false, uid, new OnResultListener() {
                        @Override
                        public void onResult(int stutas, String msg, int total, String data) {
                            if (stutas ==0){
                                UCommonUtil.getUtil().showToast(ChatPersonSettingActivity.this, "取消屏蔽成功");
                            }else {
                                UCommonUtil.getUtil().showToast(ChatPersonSettingActivity.this, "取消屏蔽失败");
                            }
                        }
                    });
                }
                break;
        }
    }

    /**
     * 从融云检查用户是否在黑名单里
     * */
    private void getBlacks(){
        RongChatManager.getUtils().getBlackById(uid, new OnResultListener() {
            @Override
            public void onResult(int stutas, String msg, int total, String data) {
                if (stutas ==0){//在黑名单
                    shield_bt.setChecked(true);
                }else {
                    isSheildInit = false;
                }
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (data != null && resultCode==1){
            remarkName = data.getStringExtra(BaseConfig.CONTENT);
            name.setText(remarkName);
        }
    }
}
